package com.qiaofang.collectmanage.task;

import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.core.logger.Logger;
import com.github.ltsopensource.core.logger.LoggerFactory;
import com.github.ltsopensource.spring.boot.annotation.JobRunner4TaskTracker;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.qiaofang.collectmanage.constants.PropEnum;
import com.qiaofang.collectmanage.dao.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;

/**
 * @author yi.li
 */
@JobRunner4TaskTracker
public class JobRunerDispatcher implements JobRunner {

    private final Logger logger = LoggerFactory.getLogger(JobRunerDispatcher.class);

    @Autowired
    CollectQualityDataRunner collectQualityRunner;
    @Autowired
    CrawlerSecretaryDataRunner crawlerSecretaryDataRunner;
    @Autowired
    CrawlerTaskCollectRuleCacheRunner crawlerTaskCollectRuleCacheRunner;

    @Override
    public Result run(JobContext jobContext) throws Throwable {
        Job job = jobContext.getJob();
        String type = job.getParam("type");
        logger.info("collectManage + type=" + type);
        JobRunner jobRunner = null;
        logger.info("JobRunerDispatcher now time:" + DateUtils.formatDate(new Date(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        //定时统计数据
        if(PropEnum.JOB_STATISTICHOUSESDATA.getCode().equals(type)){
            jobRunner = collectQualityRunner;
        } else if (PropEnum.JOB_CRAWLERSECRETARYHOUSES.getCode().equals(type)) {
            logger.info("小蜜书 + type=" + PropEnum.JOB_CRAWLERSECRETARYHOUSES.getDesc());
            jobRunner = crawlerSecretaryDataRunner;
        }else if(PropEnum.JOB_CRAWLERTASKCOLLECTRULECACHE.getCode().equals(type)){
            logger.info("定时刷新任务里的频道="+ PropEnum.JOB_CRAWLERTASKCOLLECTRULECACHE.getDesc());
            jobRunner = crawlerTaskCollectRuleCacheRunner;
        }
        return jobRunner != null ? jobRunner.run(jobContext) : null;
    }
}
